package cat.joronya.supermanager.europeo.service;

import java.util.Hashtable;
import org.htmlcleaner.ContentNode;
import org.htmlcleaner.HtmlCleaner;
import org.htmlcleaner.TagNode;

/**
 * Parse the matches of the day from ACB page.
 * http://www.acb.com/resulcla.php?codigo=LACB-56&jornada=10&resultados=1
 * 
 * @author pol
 */
public class DayMatchesParser 
{
	private static final String TAG = "supermanager";

	private int jornada; 

	// team name mapping
	private Hashtable<String, String> teamNameToAcronim = new Hashtable<String, String>();

	// matches XPATH
	private static final String MATCHES_XPATH = "//table[@bgcolor='#FFFFFF']";

	private static final String DAY1_TITLE = "JORNADA 1 - 4 DE SEPTIEMBRE";
	private static final String DAY2_TITLE = "JORNADA 2 - 5 DE SEPTIEMBRE";
	private static final String DAY3_1_TITLE = "JORNADA 3 - 6 DE SEPTIEMBRE";
	private static final String DAY3_2_TITLE = "JORNADA 3 - 7 DE SEPTIEMBRE";
	private static final String DAY4_TITLE = "JORNADA 4 - 8 DE SEPTIEMBRE";
	private static final String DAY5_TITLE = "JORNADA 5 - 9 DE SEPTIEMBRE";
	private static final String DAY6_TITLE = "2ª FASE - JORNADA 1 - 11 y 12 SEPTIEMBRE";
	private static final String DAY7_TITLE = "2ª FASE - JORNADA 2 - 13 y 14 SEPTIEMBRE";
	private static final String DAY8_TITLE = "2ª FASE - JORNADA 3 - 15 y 16 SEPTIEMBRE";
	private static final String DAY9_TITLE = "CUARTOS DE FINAL - 18 y 19 DE SEPTIEMBRE";
	private static final String DAY10_TITLE = "SEMIFINALES - 20 DE SEPTIEMBRE";
	private static final String DAY11_TITLE = "JORNADA FINAL - 22 DE SEPTIEMBRE";

	public DayMatchesParser()
	{
		teamNameToAcronim.put("Israel","ISR");
		teamNameToAcronim.put("Gran Bretaña","GBR");
		teamNameToAcronim.put("Letonia","LET");
		teamNameToAcronim.put("Bosnia","BOS");
		teamNameToAcronim.put("Georgia","GEO");
		teamNameToAcronim.put("Polonia","POL");
		teamNameToAcronim.put("Turquía","TUR");
		teamNameToAcronim.put("Finlandia","FIN");
		teamNameToAcronim.put("Bélgica","BEL");
		teamNameToAcronim.put("Ucrania","UCR");
		teamNameToAcronim.put("ARY Macedonia","MAC");
		teamNameToAcronim.put("Montenegro","MNT");
		teamNameToAcronim.put("España","ESP");
		teamNameToAcronim.put("Croacia", "CRO");
		teamNameToAcronim.put("Suecia","SUE");
		teamNameToAcronim.put("Grecia","GRE");
		teamNameToAcronim.put("Francia","FRA");
		teamNameToAcronim.put("Alemania","ALE");
		teamNameToAcronim.put("Serbia","SER");
		teamNameToAcronim.put("Lituania","LIT");
		teamNameToAcronim.put("Rep. Checa","CHC");
		teamNameToAcronim.put("Eslovenia","ESL");
		teamNameToAcronim.put("Rusia","RUS");
		teamNameToAcronim.put("Italia","ITA");
	}

	public Hashtable<String, String> parse(String html, int aJornada) throws Exception
	{
		// guardem localment la jornada que parsejem
		jornada = aJornada;

		Hashtable<String, String> matches = new Hashtable<String, String>();

		HtmlCleaner cleaner = new HtmlCleaner();
		TagNode node = cleaner.clean(html);

		// search the matches
		Object[] tables = node.evaluateXPath(MATCHES_XPATH);

		// some error and no match found
		if( tables.length == 0 )
			return null;

		for(Object otable: tables)
		{
			TagNode table = (TagNode)otable;
			TagNode tbody = table.getChildTags()[0];
			
			for(TagNode tr: tbody.getChildTags())
			{
				TagNode[] tds = tr.getChildTags();

				// get 1rst td to check if its header one
				TagNode td0 = tds[0];
				String colspan = td0.getAttributeByName("colspan");
				if( colspan != null && "6".equals(colspan) )
				{
					TagNode b0 = td0.getChildTags()[0];
					TagNode font0 = b0.getChildTags()[0];
					String title = font0.getText().toString().trim();

					// si jornada 1 nomes cerquem aquesta taula, altrament saltem
					if( jornada == 1 && !DAY1_TITLE.equals(title) )
						break;

					// si jornada 2 nomes cerquem aquesta taula, altrament saltem
					if( jornada == 2 && !DAY2_TITLE.equals(title) )
						break;

					// si jornada 3 nomes cerquem aquestes 2 taules, altrament saltem
					if( jornada == 3 && !DAY3_1_TITLE.equals(title) && !DAY3_2_TITLE.equals(title) )
						break;

					// si jornada 4 nomes cerquem aquesta taula, altrament saltem
					if( jornada == 4 && !DAY4_TITLE.equals(title) )
						break;

					// si jornada 5 nomes cerquem aquesta taula, altrament saltem
					if( jornada == 5 && !DAY5_TITLE.equals(title) )
						break;

					// si jornada 6 nomes cerquem aquesta taula, altrament saltem
					if( jornada == 6 && !DAY6_TITLE.equals(title) )
						break;

					// si jornada 7 nomes cerquem aquesta taula, altrament saltem
					if( jornada == 7 && !DAY7_TITLE.equals(title) )
						break;

					// si jornada 8 nomes cerquem aquesta taula, altrament saltem
					if( jornada == 8 && !DAY8_TITLE.equals(title) )
						break;

					// si jornada 9 nomes cerquem aquesta taula, altrament saltem
					if( jornada == 9 && !DAY9_TITLE.equals(title) )
						break;

					// si jornada 10 nomes cerquem aquesta taula, altrament saltem
					if( jornada == 10 && !DAY10_TITLE.equals(title) )
						break;

					// si jornada 11 nomes cerquem aquesta taula, altrament saltem
					if( jornada == 11 && !DAY11_TITLE.equals(title) )
						break;
					
					// saltem 1er tr un cop sabem q som a la taula q volem
					continue;
				}

				String team1 = null;
				TagNode td1 = tds[1];
				TagNode font1 = td1.getChildTags()[0];
				for(Object child: font1.getChildren())
				{
					if(child instanceof ContentNode)
					{
						String childText = ((ContentNode)child).toString().trim();
						team1 = teamNameToAcronim.get(childText);
						break;
					}
					// spain in bold tag
					TagNode bold = (TagNode)child;
					if( "b".equals(bold.getName() ) )
					{
						boolean found = false;
						for(Object boldChild: bold.getChildren())
						{
							if( boldChild instanceof ContentNode )
							{
								String boldText = ((ContentNode)boldChild).toString().trim();
								team1 = teamNameToAcronim.get(boldText);
								found = true;
								break;
							}
						}
						if(found)
							break;
					}
				}

				String team2 = null;
				TagNode td3 = tds[3];
				TagNode font3 = td3.getChildTags()[0];
				for(Object child: font3.getChildren())
				{
					if(child instanceof ContentNode)
					{
						String childText = ((ContentNode)child).toString().trim();
						if( "".equals(childText))
							continue;
						team2 = teamNameToAcronim.get(childText);
						break;
					}
					// spain in bold tag
					TagNode bold = (TagNode)child;
					if( "b".equals(bold.getName() ) )
					{
						boolean found = false;
						for(Object boldChild: bold.getChildren())
						{
							if( boldChild instanceof ContentNode )
							{
								String boldText = ((ContentNode)boldChild).toString().trim();
								team2 = teamNameToAcronim.get(boldText);
								found = true;
								break;
							}
						}
						if(found)
							break;
					}
				}

				if( team1 != null && team2 != null )
				{
					matches.put(team1,team2);
					matches.put(team2,team1);
				}
			}
		}

		return matches;
	}
}
